Grokking Dynamic Programming Patterns for Coding Interviews
Rate this course
Ask Author
Contribute
Back to course home

0% completed

Vote For New Content
Introduction
What is Dynamic Programming?
Pattern 1: 0/1 Knapsack
Introduction
0/1 Knapsack
Solution: 0/1 Knapsack
Equal Subset Sum Partition
Solution: Equal Subset Sum Partition
Subset Sum
Solution: Subset Sum
Minimum Subset Sum Difference
Solution: Minimum Subset Sum Difference
Count of Subset Sum
Solution: Count of Subset Sum
Target Sum
Solution: Target Sum
Pattern 2: Unbounded Knapsack
Unbounded Knapsack
Solution: Unbounded Knapsack
Rod Cutting
Solution: Rod Cutting
Coin Change
Solution: Coin Change
Minimum Coin Change
Solution: Minimum Coin Change
Maximum Ribbon Cut
Solution: Maximum Ribbon Cut
Pattern 3: Fibonacci Numbers
Fibonacci numbers
Solution: Fibonacci numbers
Staircase
Solution: Staircase
Number factors
Solution: Number factors
Minimum jumps to reach the end
Solution: Minimum jumps to reach the end
Minimum jumps with fee
Solution: Minimum jumps with fee
House thief
Solution: House thief
Pattern 4: Palindromic Subsequence
Longest Palindromic Subsequence
Solution: Longest Palindromic Subsequence
Longest Palindromic Substring
Solution: Longest Palindromic Substring
Count of Palindromic Substrings
Solution: Count of Palindromic Substrings
Minimum Deletions in a String to make it a Palindrome
Solution: Minimum Deletions in a String to make it a Palindrome
Palindromic Partitioning
Solution: Palindromic Partitioning
Pattern 5: Longest Common Substring
Longest Common Substring
Solution: Longest Common Substring
Longest Common Subsequence
Solution: Longest Common Subsequence
Minimum Deletions & Insertions to Transform a String into another
Solution: Minimum Deletions & Insertions to Transform a String into another
Longest Increasing Subsequence
Solution: Longest Increasing Subsequence
Maximum Sum Increasing Subsequence
Solution: Maximum Sum Increasing Subsequence
Shortest Common Super-sequence
Solution: Shortest Common Super-sequence
Minimum Deletions to Make a Sequence Sorted
Solution: Minimum Deletions to Make a Sequence Sorted
Longest Repeating Subsequence
Solution: Longest Repeating Subsequence
Subsequence Pattern Matching
Solution: Subsequence Pattern Matching
Longest Bitonic Subsequence
Solution: Longest Bitonic Subsequence
Longest Alternating Subsequence
Solution: Longest Alternating Subsequence
Edit Distance
Solution: Edit Distance
Strings Interleaving
Solution: Strings Interleaving
Contact Us
Solution: Maximum Sum Increasing Subsequence

Problem Statement

Given a number sequence, find the increasing subsequence with the highest sum. Write a method that returns the highest sum.

Example 1:

Input: {4,1,2,6,10,1,12}
Output: 32
Explanation: The increaseing sequence is {4,6,10,12}. 
Please note the difference, as the LIS is {1,2,6,10,12} which has a sum of '31'.

Example 2:

Input: {-4,10,3,7,15}
Output: 25
Explanation: The increaseing sequences are {10, 15} and {3,7,15}.

Constraints:

  • 1 <= nums.length <= 2500
  • -104 <= nums[i] <= 104

Basic Solution

The problem is quite similar to the "Longest Increasing Subsequence". The only difference is that, instead of finding the increasing subsequence with the maximum length, we need to find an increasing sequence with the maximum sum.

A basic brute-force solution could be to try all the subsequences of the given array. We can process one number at a time, so we have two options at any step:

  1. If the current number is greater than the previous number that we included, we include that number in a running sum and make a recursive call for the remaining array.
  2. We can skip the current number to make a recursive call for the remaining array.

The highest sum of any increasing subsequence would be the max value returned by the two recurse calls from the above two options.

Code

Here is the code:

Java
Saved to Cloud

. . . .

The time complexity of the above algorithm is exponential , where 'n' is the lengths of the input array. The space complexity is which is used to store the recursion stack.

Top-down Dynamic Programming with Memoization

We can use memoization to overcome the overlapping subproblems.

The three changing values for our recursive function are the current index, the previous index, and the sum. An efficient way of storing the results of the subproblems could be a hash-table whose key would be a string (currentIndex + "|" + previousIndex + "|" + sum).

Here is the code:

Java
Saved to Cloud

. . . .

Bottom-up Dynamic Programming

The above algorithm tells us two things:

  1. If the number at the current index is bigger than the number at the previous index, we include that number in the sum for an increasing sequence up to the current index.
  2. But if there is a maximum sum increasing subsequence (MSIS), without including the number at the current index, we take that.

So we need to find all the increasing subsequences for a number at index i, from all the previous numbers (i.e. numbers till index i-1), to find MSIS.

If i represents the currentIndex and 'j' represents the previousIndex, our recursive formula would look like:

    if num[i] > num[j] => dp[i] = dp[j] + num[i] if there is no bigger MSIS for 'i'

Let's draw this visually for {-4,10,3,7,15}. Start with a subsequence of length '1', as every number can represent an MSIS:

Image
Image

From the above visualization, we can clearly see that the maximum sum of any increasing subsequence is '25' -- as shown by dp[4].

Here is the code for our bottom-up dynamic programming approach:

Java
Saved to Cloud

. . . .

The time complexity of the above algorithm is and the space complexity is .

Maximum Sum Increasing Subsequence
Shortest Common Super-sequence
Mark as Completed

On this page

Problem Statement

Basic Solution

Code

Top-down Dynamic Programming with Memoization

Bottom-up Dynamic Programming